package org.libnodave.tests;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.net.Socket;
import java.text.SimpleDateFormat;
import java.util.Date;

import org.libnodave.Nodave;
import org.libnodave.PLCinterface;
import org.libnodave.TCPConnection;
public class PlcTcpClient {
	int i, j;
	long a, b, c;
	float d, e, f;
	byte buf1[];
	PLCinterface di;
	TCPConnection dc;
	Socket sock;
	PlcTcpClient(String host) {
		buf1 = new byte[Nodave.PartnerListSize];
		try {
			sock = new Socket(host, 102);
		} catch (IOException e) {
			System.out.println(e);
		}
	}
 	void run() {
		OutputStream oStream = null;
		InputStream iStream = null;
		byte[] by;
		if (sock != null) {
			try {
				oStream = sock.getOutputStream();
			} catch (IOException e) {
			}
			try {
				iStream = sock.getInputStream();
			} catch (IOException e) {
			}
			di = new PLCinterface(
					oStream,
					iStream,
					"PLCName",
					0,
					Nodave.PROTOCOL_ISOTCP);
			dc = new TCPConnection(di, 0, slot);
			int res = dc.connectPLC();
			if(di.isConnect){
			    while(true){
                    dc.readBytes(Nodave.DB, 102, 0, 50, null);
                   dc.getDINT(22);
                    System.out.println("111:"+dc.getDINT(22));
                    try {
                        Thread.sleep(2000);
                    } catch (InterruptedException e) {
                        e.printStackTrace();
                    }
			    }
			}
		} else
			System.out.println("Couldn't open connection");
	}

	static boolean doWrite = true;
	static boolean doBenchmark = false;
	static boolean doLentest = false;
	static int useProtocol = Nodave.PROTOCOL_ISOTCP;
	static int slot=1;

	public static void main(String[] args) {
//	    PlcTcpClient tp = new PlcTcpClient("192.168.8.102");
//		tp.run();
		Date t = new Date();
        SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        String dqsj;
        dqsj= "2019-10-19 22:01:48";//df.format(t);
        long longstr1 = Long.valueOf(dqsj.replaceAll("[-\\s:]",""));
        System.out.println(longstr1);
        System.out.println(longstr1+4043);
        System.out.println(dqsj);
	}
}